/**
* @mxmlc -output=bin/assets/modules/general.swf -external-library-path+=lib/library.swc -debug=false -noplay
*/
package general 
{
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	
	import general.view.General;
	import general.view.GeneralHead;
	
	import interfaces.IOperate;
	
	import org.airmvc.View;
	
	import vo.GeneralVO;
	
	/**
	 * ...
	 * @author WLDragon 2014-02-28
	 */
	public class GeneralV extends View implements IOperate
	{
		
		public function GeneralV(){}
		
		override public function init():void 
		{			
			infoPanel0 = new General();
			addChild(infoPanel0);
			
			infoPanel1 = new General();
			infoPanel1.y = 125;
			addChild(infoPanel1);
			
			head = new GeneralHead();
			head.visible = false;
			addChild(head);
			
			head2 = new GeneralHead(415);
			
			this.y = 310;
			this.scrollRect = new Rectangle(0,0,513,125);
		}
		
		override protected function addListeners():void 
		{
			receive(GeneralC.SHOW_OR_HIDE,handleShowOrHide);
			receive(GeneralC.SHOW_INFO, handleShowInfo);
			receive(GeneralC.SHOW_HEAD, handleShowHead);
		}
		
		private function handleShowHead(g:GeneralVO,g2:GeneralVO):void 
		{
			if(g2 == null)
			{
				if(contains(head2))
					removeChild(head2);
			}
			else
			{
				head2.update(g2);
				addChild(head2);
			}
			
			head.update(g);
			head.visible = true;
			infoPanel0.visible = false;
			infoPanel1.visible = false;
			handleShowOrHide(true);
		}
		
		private function handleShowInfo(gs:Array,mode:int):void 
		{
			this.mode = mode;
			this.generals = gs;
			
			if(mode == Cnt.MODE_STRATEGY || mode == Cnt.MODE_RECONNOITER)
			{
				Kyb.setOperater(this);
			}
				
			if (gs.length > 0)
			{
				infoPanel0.update(gs[0]);
				
				head.visible = false;
				infoPanel0.visible = true;
				infoPanel1.visible = true;
				handleShowOrHide(true);
			}
		}

		private function handleShowOrHide(isShow:Boolean):void 
		{
			if (isShow)
			{
				if(parent == null)
					Lyr.ui.addChild(this);
			}
			else
			{
				if(parent)
					Lyr.ui.removeChild(this);
			}
		}
		
		public function execute (keyCode:int) : void
		{
			if (isRun == false)
			{
				switch (keyCode) 
				{
					case Keyboard.W:
						direction = 1;
						index--;
					break;
					case Keyboard.S:
						direction = -1;
						index++;
					break;
					case Keyboard.R:
						send(GeneralC.CLOSE, mode);
						handleShowOrHide(false);
						infoPanel0.y = 0;
						infoPanel1.y = 125;
						_index = 0;
					break;
					case Keyboard.SPACE:
						if (mode == Cnt.MODE_RECONNOITER)
							send(GeneralC.SWITCH_CASTLE_INFO);
					break;
				}
			}
		}
		
		/**
		 * 滚动
		 */
		private function roll():void
		{
			this["infoPanel" + flag].y = -125 * direction;
			App.timer.doFrameLoop(2, onFrame);
			isRun = true;
		}
		
		private function onFrame():void 
		{
			if (this["infoPanel" + flag].y == 0)
			{
				App.timer.clearTimer(onFrame);
				isRun = false;
			}
			else
			{
				infoPanel0.y += 25 * direction;
				infoPanel1.y += 25 * direction;
			}
		}
		
		private function get index():int 
		{
			return _index;
		}
		
		private function set index(value:int):void 
		{
			if (value < 0 || value >= generals.length)
				return;
				
			flag = value % 2 >> 0;
			this["infoPanel" + flag].update(generals[value]);
			
			_index = value;
			roll();
		}
		
		private var generals:Array;
		/**武将信息面板1*/
		private var infoPanel0:General;
		/**武将信息面板2,在有多个武将可以切换查看信息时用到*/
		private var infoPanel1:General;
		/**武将头像*/
		private var head:GeneralHead;
		/**武将头像*/
		private var head2:GeneralHead;
		/**当前显示的武将序号*/
		private var _index:int;
		/**是否正在滚动*/
		private var isRun:Boolean;
		/**另一个没出现的武将信息栏*/
		private var flag:int;
		/**滚动方向1:向下 -1:向上*/
		private var direction:int;
		/**当前的模式*/
		private var mode:int;
	}
	
}